﻿using UnityEngine;
using System.Collections;

public class UpdateCameraSize : MonoBehaviour {
	public VoidDelegate _onSizeChange;
	public Camera _camera;
	public float _speed;
	public Vector2 _clamp;

	private FSM _state = new FSM();
	private StatePath _idle = new StatePath ("idle");
	private StatePath _move = new StatePath ("move");
	private float _target;
	private float _startvalue;
	// Use this for initialization
	void Start () {
		_state.AddState (_idle, OnIdleEnter);
		_state.AddState (_move, OnMoveEnter, OnMoveUpdate);
	}
	void OnIdleEnter(){
		
	}
	void OnMoveEnter(){
		
	}
	void OnMoveUpdate(float dt){
		float value;
		value = Mathf.Lerp (_startvalue, _target, _state.stateTime * _speed);
		_camera.orthographicSize = value;
		if (_onSizeChange != null)
			_onSizeChange ();
		if (value == _target)
			_state.SetState (_idle);
	}
	
	// Update is called once per frame
	void Update () {
		_state.UpdateState (Time.deltaTime);
	}

	public void UpdateTo(float value){
		_target = value;
		_startvalue = _camera.orthographicSize;
		_state.SetState (_move);

	}
	public float GetSize(){
		return _camera.orthographicSize;
	}
	public void SetSize(float value){
		value = Mathf.Clamp (value, _clamp.x, _clamp.y);
		_camera.orthographicSize = value;
		if (_onSizeChange != null)
			_onSizeChange ();
	}
}
